package edu.ustb.service.goods.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.ustb.domain.goods.Goods;
import edu.ustb.mapper.goods.GoodsDao;
import edu.ustb.service.goods.GoodsListService;
import edu.ustb.utils.MybatisUtil;

import java.util.ArrayList;
import java.util.List;

public class  GoodsListServiceImpl implements GoodsListService {

    @Override
    public List<Goods> findall() {
        GoodsDao goodsMapper = MybatisUtil.getMapper(GoodsDao.class);
        List<Goods> goodsList = goodsMapper.findAll();
        System.out.println(goodsList);
        MybatisUtil.close();
        return goodsList;

    }

    @Override
    public PageInfo<Goods> getPageGoodsByPageNum(int pageNum) {
        // 获取 GoodsDao 的 mapper 对象以便后续查询
        GoodsDao goodsMapper = MybatisUtil.getMapper(GoodsDao.class);
        // 设置分页信息（该操作一定要在请求所有数据之前）
        // pageNum : 当前的页数    pageSize : 每页显示的数据的数量
        PageHelper.startPage(pageNum,8);
        ArrayList<Goods> allGoods = (ArrayList<Goods>) goodsMapper.findAll();
        // 生成相应的分页数据
        PageInfo<Goods> pageInfo = new PageInfo<>(allGoods);
        // 将页面数据返回
        return pageInfo;
    }

    @Override
    public Goods getGoodsDetailsByGoodsId(int goodsId) {
        // 获取 GoodsDao 的 mapper 对象以便后续查询
        GoodsDao goodsMapper = MybatisUtil.getMapper(GoodsDao.class);
        // 按指定 Id 获取商品对象

        Goods goods = goodsMapper.selectByPrimaryKey(goodsId);


        // 将页面数据返回
        return goods;
    }
}
